<?php
class Localization
{

    const SESSION_LOCAL_PARAMETER = "local_code";
    const LANGUAGE_CODE_COLUMN = "code";
    const LANGUAGE_NAME_COLUMN = "name";
    const LANGUAGE_LOCAL_VALUE_COLUMN = "localValue";
    const LANGUAGE_LIST = "languageList";
    const SHORT_LIST = "list";
    const CURRENT_LANGUAGE = "current";
    const LANGUAGES = "languages";
    const ASSETS = "assets";
    const NUMBER = "number";
    const PAGES = "pages";
    const PRIVILEGES = "privileges";
    const KONTROLORI = "kontrolori";

    const XML_LANGUAGE_LIST = "languagesList.xml";
    const XML_PAGE_LIST = "pagesList.xml";
    const XML_ASSETS = "assets.xml";
    const XML_VERIFIKACIJE = "verifikacije.xml";

    private static $instance = null;
    private static $default_language = null;
    private static $languages_list = null;
    private static $pages_list = null;
    private static $verifikacije = null;

    final static function getInstance()
    {
        if (self::$instance == null) {
            self::$instance = new Localization();
        }
        return self::$instance;
    }

    private function __construct()
    {
        if (!isset($_SESSION[self::SESSION_LOCAL_PARAMETER])) {
            if (isset($_COOKIE[self::SESSION_LOCAL_PARAMETER])) {
                $_SESSION[self::SESSION_LOCAL_PARAMETER] = $_COOKIE[self::SESSION_LOCAL_PARAMETER];
                setcookie(self::SESSION_LOCAL_PARAMETER, $_SESSION[self::SESSION_LOCAL_PARAMETER], time() + 30 * MemcacheFetcher::DAY);
            } else if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
                $_SESSION[self::SESSION_LOCAL_PARAMETER] = self::getDefaultLanguage();
                $languages = $this->getLanguages();
                Log::logInfo(json_encode($languages));
                foreach ($languages as $value) {
                    if (substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2) == $value[self::LANGUAGE_CODE_COLUMN]) {
                        $_SESSION[self::SESSION_LOCAL_PARAMETER] = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
                        break;
                    }
                }
            } else {
                $_SESSION[self::SESSION_LOCAL_PARAMETER] = self::getDefaultLanguage();
            }
        }
        ;
    }

    public function get()
    {
        return $this->buildResult();
    }

    public function set()
    {
        $params = json_decode($_REQUEST[Actions::PARAMETERS_NAME], true);
        $this->setCurrentLanguage($params[self::LANGUAGE_CODE_COLUMN]);
        return $this->buildResult();
    }

    private function buildResult()
    {
        return array(Modal::STATUS => Modal::STATUS_OK,
            Modal::RESPONSE_PARAMETERS => array(
                self::LANGUAGES => array(
                    self::SHORT_LIST => $this->getLanguages(),
                    self::CURRENT_LANGUAGE => $this->getCurrentLanguage(),
                ),
                self::ASSETS => $this->getAllAssets(),
                self::PAGES => array(
                    self::SHORT_LIST => $this->getPages()
                ),
                self::PRIVILEGES => array(
                    self::SHORT_LIST => User::getAllPrivileges()
                ),
                self::KONTROLORI => array(
                    self::SHORT_LIST => User::getAllKontrolori()
                ),
                "verifikacije" => array(
                    self::SHORT_LIST => $this->getVerifikacije()
                ),
                "opstine" => User::getAllOpstina()
            ));
    }

    public function setCurrentLanguage($language)
    {
        $_SESSION[self::SESSION_LOCAL_PARAMETER] = $language;
    }

    public function getCurrentLanguage()
    {
        $languages = $this->getLanguages();
        foreach ($languages as $value) {
            if ($_SESSION[self::SESSION_LOCAL_PARAMETER] == $value[self::LANGUAGE_CODE_COLUMN]) {
                return $value;
            }
        }
        return array();
    }

    public function getLanguages()
    {
        if (self::$languages_list == null) {
            $xml = XmlFetcher::getInstance()->getXml(self::XML_LANGUAGE_LIST);
            $list = array();
            foreach ($xml->language as $language) {
                $list[count($list)] = array(
                    self::LANGUAGE_CODE_COLUMN => (string)$language->attributes()->{self::LANGUAGE_CODE_COLUMN},
                    self::LANGUAGE_NAME_COLUMN => (string)$language->attributes()->{self::LANGUAGE_NAME_COLUMN}
                );
            }
            self::$languages_list = $list;
        }
        return self::$languages_list;
    }

    public function getPages()
    {
        if (self::$pages_list == null) {
            $xml = XmlFetcher::getInstance()->getXml(self::XML_PAGE_LIST);
            $list = array();
            foreach ($xml->page as $page) {
                $list[count($list)] = array(
                    self::NUMBER => (string)$page->number,
                );
            }
            self::$pages_list = $list;
        }
        return self::$pages_list;
    }
    
    public function getVerifikacije()
    {
    	if (self::$verifikacije == null) {
    		$xml = XmlFetcher::getInstance()->getXml(self::XML_VERIFIKACIJE);
    		$list = array();
    		foreach ($xml->verifikacija as $page) {
    			$list[count($list)] = array(
    			"number" => (string)$page->number,
    			"text" => (string)$page->text
    			);
    		}
    		self::$verifikacije = $list;
    	}
    	return self::$verifikacije;
    }

    public function getAsset($asset)
    {
        $list = $this->getAllAssets();
        foreach ($list[self::SHORT_LIST] as $value) {
            if ($value[self::LANGUAGE_NAME_COLUMN] == $asset)
                return $value[self::LANGUAGE_LOCAL_VALUE_COLUMN];
        }
        return "";
    }

    public function getAllAssets()
    {
        $xml = XmlFetcher::getInstance()->getXml($_SESSION[self::SESSION_LOCAL_PARAMETER] . "/" . self::XML_ASSETS);
        $list = array();
        foreach ($xml->asset as $asset) {
            $list[count($list)] = array(
                self::LANGUAGE_LOCAL_VALUE_COLUMN => (string)$asset->attributes()->{self::LANGUAGE_LOCAL_VALUE_COLUMN},
                self::LANGUAGE_NAME_COLUMN => (string)$asset->attributes()->{self::LANGUAGE_NAME_COLUMN}
            );
        }
        return array(self::SHORT_LIST => $list);
    }


    public static function getDefaultLanguage()
    {
        self::$default_language = "sr";
        return self::$default_language;
    }
}
